package bubuntux.jusbscan.core;

import java.util.ArrayList;

import bubuntux.jusbscan.tui.Frame;

public class TerminalContainer extends Thread {

	private ArrayList<Terminal> terminals = null;

	public TerminalContainer() {
		this(Configs.terminalCount);
	}

	public TerminalContainer(int countTerminal) {
		super();

		this.terminals = new ArrayList<Terminal>();

		for (int i = 0; i < countTerminal; i++) {
			this.terminals.add(new Terminal(((char) (i + 97)) + "")); // 'a'=97
		}

	}

	public ArrayList<Frame> getFrames() {
		ArrayList<Frame> frames = new ArrayList<Frame>();
		for (Terminal terminal : terminals) {
			frames.add(terminal.getFrame());
		}
		return frames;
	}

	public void printTerminals() {
		for (Terminal terminal : this.terminals) {
			if (terminal.isAlive()) {
				terminal.getFrame().repaint();
			}
		}
	}

	@Override
	public void run() {
		this.terminals.get(0).start();

		while (true) { // infity cycle

			printTerminals();

			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

}
